// 字符串有三种编辑操作:插入一个字符、删除一个字符或者替换一个字符。 
// 给定两个字符串，编写一个函数判定它们是否只需要一次(或者零次)编辑。

// 思路 顺序遍历两个字符串，如果同一下标值不同，diff++，diff>=2的时候false

/**
 * @param {string} first
 * @param {string} second
 * @return {boolean}
 */
 var oneEditAway = function(first, second) {
    console.log("first:",first,"second:",second);
    if(first === second || first.length===1&&second.length===1){
        return true;
    }
    let diff = 0;
    if(first.length !== second.length){//长度不相等的情况，分出长短
        let long = first.length > second.length ? first : second;
        let short =  first.length < second.length ? first : second;
        console.log("long:",long,"short:",short);
        
        // 同时遍历两个字符串，比较对应下标处的字符是否相同，如果字符相同则将两个字符串的下标同时加  11，
        // 如果字符不同则只将 \textit{longer}longer 的下标加 11。
        // 遍历过程中如果出现两个字符串的下标之差大于 11 则不符合一次编辑，
        // 遍历结束时如果两个字符串的下标之差不大于 11 则符合一次编辑。
        
        let i = 0, j = 0;// i较短数组下标，j较长数组下表


        if(long.length - 1 === short.length){
            for( i = 0,j=0;i<short.length&&j<long.length;){
                if(short[i] === long[j]){
                    i++;
                    j++;
                    continue;
                }else{
                    j++;
                }
            }
        }else{// 长度差距超过2直接 返回false
            return false;
        }

        if( Math.abs( j-i ) > 1 ){
            return false
        }

    }else{//长度相等的情况
        for(let i = 0;i<second.length ;i++){
            if( first[i] === second[i]){
                continue;
            }else{
                diff++;
            }
        }
    }
    console.log(diff);
    return diff > 1 ? false : true;
 }

// let first = "pale",second = "ple"
// let first = "horse",second = "ros"
// let first = "a",second = "b"
// let first = "teacher",second = "beacher"
// let first = "ab",second = "bc"
// let first = "sea",second = "ate"// false
// let first = "prosperity",second = "properties"//false
// let first = "islander",second = "slander"
let first = "mart",second = "karma"


// console.log(oneEditAway(first,second));
console.log(1+false);
console.log(1+true);
console.log(true+'10');
console.log(true+10);
console.log(100 == '100');// true